package com.mingmen.canting.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.mingmen.canting.R;
import com.mingmen.canting.activity.ForgetActivity;
import com.mingmen.canting.activity.LoginActivity;
import com.mingmen.canting.base.BaseBean;
import com.mingmen.canting.dialog.ButtomDialogView;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.immersionbar.ImmersionBar;
import com.mingmen.canting.personal.DailyListActivity;
import com.mingmen.canting.personal.PersonalNoticeListActivity;
import com.mingmen.canting.personal.PersonalSettingsActivity;
import com.mingmen.canting.personal.PersonalShenHeActivity;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;


public class PersonalFragment extends Fragment {
    @BindView(R.id.text_return)
    TextView textReturn;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.tv_right)
    TextView tvRight;

    @BindView(R.id.mToolbar)
    Toolbar mToolbar;
    @BindView(R.id.text_center)
    TextView text_center;
    @BindView(R.id.linear_return)
    LinearLayout linear_return;
    @BindView(R.id.layout_shenhe)
    LinearLayout layout_shenhe;
    @BindView(R.id.layout_centerNotice)
    LinearLayout layout_centerNotice;
    @BindView(R.id.layout_ribao)
    LinearLayout layout_ribao;
    Unbinder unbinder;
    @BindView(R.id.btn_login)
    Button btn_login;
    @BindView(R.id.layout_resetpwd)
    LinearLayout layout_resetpwd;
    ButtomDialogView bottomSheetDialog;
    @BindView(R.id.ivAvatar)
    ImageView ivAvatar;
    public static final int REQUEST_CODE_SELECT = 100;
    public static final int REQUEST_CODE_PREVIEW = 101;
    ArrayList<ImageItem> images = new ArrayList<>();
    private ArrayList<ImageItem> selImageList = new ArrayList<>(); //当前选择的所有图片
    String token;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_refresh_practive, null);
        unbinder = ButterKnife.bind(this, view);
        textReturn.setVisibility(View.GONE);
        text_center.setText("我的");
        ImmersionBar.with(this).titleBar(mToolbar)
                .navigationBarColor(R.color.black).init();
        createDialog();
        token = (String) SharePreferenceUtil.get(getActivity(), "token", "");
        return view;
//        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.text_return, R.id.img_right, R.id.tv_right, R.id.linear_return, R.id.layout_shenhe, R.id.layout_centerNotice, R.id.layout_ribao, R.id.btn_login, R.id.layout_resetpwd, R.id.ivAvatar})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.text_return:
            case R.id.linear_return:


                break;
            case R.id.img_right:
                break;
            case R.id.tv_right:
                break;
            case R.id.layout_shenhe:
                startActivity(new Intent(getActivity(), PersonalShenHeActivity.class));
                break;
            case R.id.layout_centerNotice:
                startActivity(new Intent(getActivity(), PersonalNoticeListActivity.class));
                break;
            case R.id.layout_ribao:
                startActivity(new Intent(getActivity(), DailyListActivity.class));
                break;
            case R.id.btn_login:
                quid(token);

                break;
            case R.id.layout_resetpwd:
//                startActivity(new Intent(getActivity(), ForgetActivity.class));
                intent = new Intent(getActivity(), ForgetActivity.class);
                intent.putExtra("flag", "1");
                startActivity(intent);
                break;
            case R.id.ivAvatar:
//                bottomSheetDialog.show();
                intent = new Intent(getActivity(), ImageGridActivity.class);
                intent.putExtra(ImageGridActivity.EXTRAS_IMAGES, images);
                //ImagePicker.getInstance().setSelectedImages(images);
                startActivityForResult(intent, REQUEST_CODE_PREVIEW);
                break;
        }
    }

    public void quid(String token) {
        HttpResponse.quit(token, new StringCallback(getActivity()) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                BaseBean baseBean = new BaseBean();
                baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                if (baseBean.getCode() == 0) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                } else {
                    Toast.makeText(getActivity(), baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void createDialog() {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View view1 = layoutInflater.inflate(R.layout.dialog_bottom_send, null);

        bottomSheetDialog = new ButtomDialogView(getActivity(), view1, true, true);


        TextView text_dismiss = view1.findViewById(R.id.text_dismiss);
        text_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetDialog.dismiss();
            }
        });

        ImageView img_camera = view1.findViewById(R.id.img_camera);
        img_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ImageGridActivity.class);
                intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true); // 是否是直接打开相机
                startActivityForResult(intent, REQUEST_CODE_SELECT);
                bottomSheetDialog.dismiss();
            }
        });

        ImageView img_calumb = view1.findViewById(R.id.img_calumb);
        img_calumb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ImageGridActivity.class);
                intent.putExtra(ImageGridActivity.EXTRAS_IMAGES, images);
                //ImagePicker.getInstance().setSelectedImages(images);
                startActivityForResult(intent, REQUEST_CODE_PREVIEW);
                bottomSheetDialog.dismiss();
            }
        });
    }


    List<File> myfile = new ArrayList<>();
    String path;
    File file;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == REQUEST_CODE_SELECT) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                for (int i = 0; i < images.size(); i++) {
                    Log.d("dkdkdkk", images.get(i).path);
                    path = images.get(i).path;
                    file = new File(path);
                    myfile.add(file);
                }
                Bitmap bm = BitmapFactory.decodeFile(images.get(0).path);
                ivAvatar.setImageBitmap(bm);
            }


        } else if (resultCode == ImagePicker.RESULT_CODE_BACK) {
            //预览图片返回
            if (data != null && requestCode == REQUEST_CODE_PREVIEW) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_IMAGE_ITEMS);

            }
        }
    }

}
